//
//  HTThankViewController.m
//  HTDemo
//
//  Created by meet on 14-3-14.
//  Copyright (c) 2014年 HT. All rights reserved.
//

#import "HTThankViewController.h"

static NSString * kHTWords = @"\t感谢您打开了我的源码并且运行起来。我是来自电子科技大学网络工程专业大三的胡腾。\n\t收到内推链接后突然发现需要上传作品，由于保密协议，非常遗憾我不能给您提供已做项目的源码，所以加紧进度做了Demo，并抽取了几个以前写的类放在了这个Demo里，希望能窥其一斑，大致了解我的编码风格与编码态度。\n\t谈谈我的优势与劣势。优势是有独立开发项目的能力，Objective-C基础扎实，实践证明喜欢这一行。劣势是非计算机科班出身，编程起步晚，在知识的广度上不如很多同学，数据库与Java几乎空白。\n\t对自己，我尽量做到客观评价，具体我给您的印象，还是得由您来决定啦。最后再感谢您仔细的阅读了这段文字:），并且由衷希望您能对于我的代码提出一些建议与反馈，我一定努力汲取，感谢。";
static CGFloat const kMargin = 10;
static CGFloat const kHeadRadius = 80;
@interface HTThankViewController ()

@end

@implementation HTThankViewController
{
    UIScrollView *_scrollView;
}

#pragma mark - 生命周期

- (void)viewDidLoad
{
 
    [super viewDidLoad];
    self.navigationItem.title = @"一些想说的话";
    self.view.backgroundColor = [UIColor whiteColor];
    [self configScrollView];
    [self drawHead];
    [self drawWordLabel];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

#pragma mark - 构建视图

- (void)configScrollView
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - NavigationBar_Height - StateBar_Height)];
    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, _scrollView.frame.size.height);
    [self.view addSubview:_scrollView];
}

- (void)drawHead
{
    UIImageView *head = [[UIImageView alloc] initWithFrame:CGRectMake((Screen_Width - 2*kHeadRadius)/2, kMargin, kHeadRadius*2, kHeadRadius*2)];
    head.image = [UIImage imageNamed:@"head"];
    head.layer.cornerRadius = kHeadRadius;
    head.layer.masksToBounds = YES;
    [_scrollView addSubview:head];
}

- (void)drawWordLabel
{
    CGFloat fontSize = 20;
    
    UILabel *wordLabel = [[UILabel alloc] init];
    wordLabel.text = kHTWords;
    wordLabel.font = [UIFont systemFontOfSize:fontSize];
    wordLabel.textColor = COLOR_INFO;
    wordLabel.lineBreakMode = NSLineBreakByWordWrapping;
    wordLabel.numberOfLines = 0;
    CGSize wordSize = [self calculateTheLabelSizeWithFontSize:fontSize];
    wordLabel.frame = CGRectMake(kMargin, 2*kMargin + 2*kHeadRadius, wordSize.width - 2*kMargin, wordSize.height);
    [_scrollView addSubview:wordLabel];
}
#pragma mark - 计算高度
- (CGSize)calculateTheLabelSizeWithFontSize:(CGFloat)fontSize
{
    CGSize maxLabelSize = CGSizeMake(Screen_Width, Screen_Height * 2);
    NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:fontSize]
                                                                 forKey:NSFontAttributeName];
    CGSize newLabelSize = [kHTWords boundingRectWithSize:maxLabelSize
                                                 options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin
                                              attributes:stringAttributes
                                                 context:nil].size;
    [self calculateTheScrollViewContentSize:newLabelSize.height];
    return newLabelSize;
}

- (void)calculateTheScrollViewContentSize:(CGFloat)labelHeight
{
    CGFloat contentSizeHeight = 2*kMargin + kHeadRadius*2 + labelHeight;
    HTLogRect(_scrollView.frame);
    if(contentSizeHeight < _scrollView.contentSize.height){
        
        return;
    }
    
    HTLogRect(_scrollView.frame);
    _scrollView.contentSize = CGSizeMake(Screen_Width, contentSizeHeight+kMargin);
}

@end
